<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/22
 * Time: 14:05
 */

namespace app\admin\controller;
use app\admin\model\SearchPrice as SearchPriceModel;

class SearchPriceController extends BaseController
{
    public function index()
    {
        $this->assign([
            'searchPrices' => SearchPriceModel::order(['id'=>'asc'])->paginate(15,false,['type'=>'page\Page']),
        ]);
        return $this->fetch();
    }

    public function add()
    {
        if($this->request->isGet()){
            return $this->fetch();
        }
        if($this->request->isPost()){
            $params = $this->request->param();
            $model = new SearchPriceModel();
            if($model->allowField(true)->isUpdate(false)->save($params)){
                return ['error'=>0,'msg'=>'操作成功!'];
            }else{
                return ['error'=>1,'msg'=>'操作失败!'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作!'];
    }

    public function edi()
    {
        $params  = $this->request->param();
        if($this->request->isGet()){
            $this->assign([
                'searchPrice' => SearchPriceModel::find($params['id']),
            ]);
            return $this->fetch();
        }
        if($this->request->isPost()){
            $model = new SearchPriceModel();
            if($model->allowField(true)->isUpdate(true)->save($params)){
                return ['error'=>0,'msg'=>'操作成功!'];
            }else{
                return ['error'=>1,'msg'=>'操作失败!'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作!'];
    }

    public function del()
    {
        $id = $this->request->param('id');
        if(SearchPriceModel::where(['id'=>$id])->delete()){
            return ['error'=>0,'msg'=>'删除成功!'];
        }else{
            return ['error'=>1,'msg'=>'删除失败!'];
        }
    }
}